<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 6节课入门Flask框架web开发 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>数据库基本操作 | 6节课入门Flask框架web开发</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html" />
    
    
    <link rel="prev" href="../chapter3/shu-ju-ku-de-she-zhi.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.2"
        data-chapter-title="数据库基本操作"
        data-filepath="chapter3/shu-ju-ku-ji-ben-cao-zuo.md"
        data-basepath=".."
        data-revision="Sun Jan 28 2018 01:59:46 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask快速入门课程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter1.html">
            
                
                    <a href="../chapter1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Flask基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="chapter1/flaskjian-jie.html">
            
                
                    <a href="../chapter1/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        为什么要使用WEB框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="chapter1/flaskyu-django-dui-bi.html">
            
                
                    <a href="../chapter1/flaskyu-django-dui-bi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="chapter1/flask-an-zhuang-huan-jing.html">
            
                
                    <a href="../chapter1/flask-an-zhuang-huan-jing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        Flask 安装环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="chapter1/requirements-wen-jian.html">
            
                
                    <a href="../chapter1/requirements-wen-jian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        requirements 文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="chapter1/hello-worldcheng-xu.html">
            
                
                    <a href="../chapter1/hello-worldcheng-xu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        Hello World程序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
            
                
                    <a href="../chapter1/lu-you-de-ge-zhong-ding-yi-fang-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        路由定义的基本方式
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter2.html">
            
                
                    <a href="../chapter2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="chapter2/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../chapter2/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Jinja2模板引擎
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="chapter2/guo-lv-qi.html">
            
                
                    <a href="../chapter2/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="chapter2/hong.html">
            
                
                    <a href="../chapter2/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Flask-WTF表单
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter3.html">
            
                
                    <a href="../chapter3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="chapter3/shu-ju-ku-de-she-zhi.html">
            
                
                    <a href="../chapter3/shu-ju-ku-de-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Flask中使用数据库
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
            
                
                    <a href="../chapter3/shu-ju-ku-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        数据库基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        定义模型&amp;创建表&amp;数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        数据显示&amp;表单添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        添加数据的表单验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.4.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >6节课入门Flask框架web开发</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x6570;&#x636E;&#x5E93;&#x57FA;&#x672C;&#x64CD;&#x4F5C;">&#x6570;&#x636E;&#x5E93;&#x57FA;&#x672C;&#x64CD;&#x4F5C;</h1>
<h2 id="&#x4E00;-&#x589E;&#x5220;&#x6539;&#x64CD;&#x4F5C;">&#x4E00;. &#x589E;&#x5220;&#x6539;&#x64CD;&#x4F5C;</h2>
<h3 id="1-&#x57FA;&#x672C;&#x6982;&#x5FF5;">1. &#x57FA;&#x672C;&#x6982;&#x5FF5;</h3>
<ul>
<li><p>&#x5728;Flask-SQLAlchemy&#x4E2D;&#xFF0C;&#x63D2;&#x5165;&#x3001;&#x4FEE;&#x6539;&#x3001;&#x5220;&#x9664;&#x64CD;&#x4F5C;&#xFF0C;&#x5747;&#x7531;&#x6570;&#x636E;&#x5E93;&#x4F1A;&#x8BDD;&#x7BA1;&#x7406;&#x3002;</p>
<ul>
<li>&#x4F1A;&#x8BDD;&#x7528;db.session&#x8868;&#x793A;&#x3002;&#x5728;&#x51C6;&#x5907;&#x628A;&#x6570;&#x636E;&#x5199;&#x5165;&#x6570;&#x636E;&#x5E93;&#x524D;&#xFF0C;&#x8981;&#x5148;&#x5C06;&#x6570;&#x636E;&#x6DFB;&#x52A0;&#x5230;&#x4F1A;&#x8BDD;&#x4E2D;&#x7136;&#x540E;&#x8C03;&#x7528; commit() &#x65B9;&#x6CD5;&#x63D0;&#x4EA4;&#x4F1A;&#x8BDD;&#x3002;</li>
</ul>
</li>
<li><p>&#x5728;Flask-SQLAlchemy&#x4E2D;&#xFF0C;&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;&#x662F;&#x901A;&#x8FC7;query&#x5BF9;&#x8C61;&#x64CD;&#x4F5C;&#x6570;&#x636E;&#x3002;</p>
<ul>
<li>&#x6700;&#x57FA;&#x672C;&#x7684;&#x67E5;&#x8BE2;&#x662F;&#x8FD4;&#x56DE;&#x8868;&#x4E2D;&#x6240;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FC7;&#x6EE4;&#x5668;&#x8FDB;&#x884C;&#x66F4;&#x7CBE;&#x786E;&#x7684;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#x3002;</li>
</ul>
</li>
</ul>
<pre><code>db.session.add(role)    &#x6DFB;&#x52A0;&#x5230;&#x6570;&#x636E;&#x5E93;&#x7684;session&#x4E2D;
db.session.add_all([user1, user2]) &#x6DFB;&#x52A0;&#x591A;&#x4E2A;&#x4FE1;&#x606F;&#x5230;session&#x4E2D;
db.session.commit()     &#x63D0;&#x4EA4;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4FEE;&#x6539;(&#x5305;&#x62EC;&#x589E;/&#x5220;/&#x6539;)
db.session.rollback()   &#x6570;&#x636E;&#x5E93;&#x7684;&#x56DE;&#x6EDA;&#x64CD;&#x4F5C;
db.session.delete(user) &#x5220;&#x9664;&#x6570;&#x636E;&#x5E93;(&#x9700;&#x8DDF;&#x4E0A;commit)
</code></pre><h3 id="2-&#x793A;&#x4F8B;">2. &#x793A;&#x4F8B;</h3>
<h4 id="21-&#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;">2.1 &#x5728;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x6A21;&#x578B;&#x7C7B;</h4>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask
<span class="hljs-keyword">from</span> flask_sqlalchemy <span class="hljs-keyword">import</span> SQLAlchemy


app = Flask(__name__)

<span class="hljs-comment">#&#x8BBE;&#x7F6E;&#x8FDE;&#x63A5;&#x6570;&#x636E;&#x5E93;&#x7684;URL</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_DATABASE_URI&apos;</span>] = <span class="hljs-string">&apos;mysql://root:mysql@127.0.0.1:3306/Flask_test&apos;</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_TRACK_MODIFICATIONS&apos;</span>] = <span class="hljs-keyword">False</span>
<span class="hljs-comment">#&#x67E5;&#x8BE2;&#x65F6;&#x4F1A;&#x663E;&#x793A;&#x539F;&#x59CB;SQL&#x8BED;&#x53E5;</span>
app.config[<span class="hljs-string">&apos;SQLALCHEMY_ECHO&apos;</span>] = <span class="hljs-keyword">True</span>

db = SQLAlchemy(app)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Role</span><span class="hljs-params">(db.Model)</span>:</span>
    <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x8868;&#x540D;</span>
    __tablename__ = <span class="hljs-string">&apos;roles&apos;</span>
    <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x5217;&#x5BF9;&#x8C61;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">32</span>), unique=<span class="hljs-keyword">True</span>)
    user = db.relationship(<span class="hljs-string">&apos;User&apos;</span>, backref=<span class="hljs-string">&apos;role&apos;</span>)

    <span class="hljs-comment">#repr()&#x65B9;&#x6CD5;&#x663E;&#x793A;&#x4E00;&#x4E2A;&#x53EF;&#x8BFB;&#x5B57;&#x7B26;&#x4E32;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&lt;Role: %s %s&gt;&apos;</span> % (self.name, self.id)

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(db.Model)</span>:</span>
    __tablename__ = <span class="hljs-string">&apos;users&apos;</span>
    id = db.Column(db.Integer, primary_key=<span class="hljs-keyword">True</span>)
    name = db.Column(db.String(<span class="hljs-number">32</span>), unique=<span class="hljs-keyword">True</span>, index=<span class="hljs-keyword">True</span>)
    email = db.Column(db.String(<span class="hljs-number">32</span>),unique=<span class="hljs-keyword">True</span>)
    password = db.Column(db.String(<span class="hljs-number">32</span>))
    role_id = db.Column(db.Integer, db.ForeignKey(<span class="hljs-string">&apos;roles.id&apos;</span>))

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__repr__</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&lt;User: %s %s %s %s&gt;&apos;</span> % (self.name, self.id, self.email, self.password)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&apos;__main__&apos;</span>:

    <span class="hljs-comment"># &#x521B;&#x5EFA;&#x8868;&#xFF1A;</span>
    db.create_all()
    <span class="hljs-comment"># &#x5220;&#x9664;&#x8868;</span>
    db.drop_all()

    app.run(debug=<span class="hljs-keyword">True</span>)
</code></pre>
<pre><code>#### 2.2 &#x6570;&#x636E;&#x7684;&#x589E;&#x5220;&#x6539;
```python
# &#x8FDB;&#x5165;ipython&#x4E00;&#x6B21;&#x6267;&#x884C;
In [1]: from demo3_sqlalchemy import *

# &#x6DFB;&#x52A0;&#x4E00;&#x6761;Role&#x6570;&#x636E;
In [2]: role = Role(name=&apos;admin&apos;)
In [3]: db.session.add(role)
In [4]: db.session.commit()

# &#x6DFB;&#x52A0;&#x4E00;&#x6761;User&#x6570;&#x636E;, &#x6570;&#x636E;&#x6709;&#x8BEF;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x56DE;&#x6EDA;, &#x5C06;add&#x7684;&#x5BF9;&#x8C61;&#x4ECE;session&#x79FB;&#x9664;
In [5]: user = User(name=&apos;zhangsan&apos;)
In [6]: db.session.add(user)
In [7]: db.session.rollback()
In [9]: user.role_id = 1
In [6]: db.session.add(user)
In [4]: db.session.commit()

# &#x4FEE;&#x6539;&#x6570;&#x636E;
In [13]: user.name = &apos;lisi&apos;
In [14]: db.session.commit()

# &#x5220;&#x9664;&#x6570;&#x636E;
In [16]: db.session.delete(user)
In [17]: db.session.commit()
</code></pre><h3 id="3-&#x6A21;&#x578B;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x8054;">3 &#x6A21;&#x578B;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x8054;</h3>
<h4 id="31-&#x4E00;&#x5BF9;&#x591A;">3.1 &#x4E00;&#x5BF9;&#x591A;</h4>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Role</span><span class="hljs-params">(db.Model)</span>:</span>
...
<span class="hljs-comment">#&#x5173;&#x952E;&#x4EE3;&#x7801;</span>
user = db.relationship(<span class="hljs-string">&apos;User&apos;</span>, backref=<span class="hljs-string">&apos;role&apos;</span>)
...

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(db.Model)</span>:</span>
...
role_id = db.Column(db.Integer, db.ForeignKey(<span class="hljs-string">&apos;roles.id&apos;</span>))
</code></pre>
<ul>
<li>&#x5176;&#x4E2D;realtionship&#x63CF;&#x8FF0;&#x4E86;Role&#x548C;User&#x7684;&#x5173;&#x7CFB;&#x3002;&#x5728;&#x6B64;&#x6587;&#x4E2D;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x4E3A;&#x5BF9;&#x5E94;&#x53C2;&#x7167;&#x7684;&#x7C7B;&quot;User&quot;</li>
<li>&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;backref&#x4E3A;&#x7C7B;User&#x7533;&#x660E;&#x65B0;&#x5C5E;&#x6027;&#x7684;&#x65B9;&#x6CD5;</li>
</ul>
<pre><code class="lang-pythn">In [1]: from demo3_sqlalchemy import *

In [2]: role = Role(name=&apos;admin&apos;)

In [3]: db.session.add(role)

In [4]: db.session.commit()

In [5]: user1 = User(name=&apos;zs&apos;, role_id=role.id)

In [6]: user2 = User(name=&apos;ls&apos;, role_id=role.id)

In [7]: db.session.add_all([user1, user2])

In [8]: db.session.commit()


# &#x6B64;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x89D2;&#x8272;&#x76F4;&#x63A5;&#x67E5;&#x8BE2;&#x5230;&#x7528;&#x6237;&#x4FE1;&#x606F;
In [9]: role.users
Out[9]: [<User: 1="" zs="" None="">, <User: 2="" ls="" None="">]

# &#x6B64;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x7528;&#x6237;&#x76F4;&#x63A5;&#x67E5;&#x8BE2;&#x5230;&#x89D2;&#x8272;&#x4FE1;&#x606F;
In [10]: user1.role
Out[10]: <Role: 1="" admin="">

In [11]: user2.role
Out[11]: <Role: 2="" admin="">

# &#x6B64;&#x65F6;&#x5C31;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x89D2;&#x8272;&#x76F4;&#x63A5;&#x67E5;&#x8BE2;&#x5230;&#x7528;&#x6237;&#x4FE1;&#x606F;
</Role:></Role:></User:></User:></code></pre>
<h2 id="&#x4E8C;-&#x67E5;&#x8BE2;&#x64CD;&#x4F5C;">&#x4E8C;. &#x67E5;&#x8BE2;&#x64CD;&#x4F5C;</h2>
<h3 id="1-&#x57FA;&#x672C;&#x6982;&#x5FF5;">1. &#x57FA;&#x672C;&#x6982;&#x5FF5;</h3>
<h3 id="11-&#x5E38;&#x7528;&#x7684;sqlalchemy&#x67E5;&#x8BE2;&#x8FC7;&#x6EE4;&#x5668;">1.1 &#x5E38;&#x7528;&#x7684;SQLAlchemy&#x67E5;&#x8BE2;&#x8FC7;&#x6EE4;&#x5668;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x8FC7;&#x6EE4;&#x5668;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">filter()</td>
<td style="text-align:left">&#x628A;&#x8FC7;&#x6EE4;&#x5668;&#x6DFB;&#x52A0;&#x5230;&#x539F;&#x67E5;&#x8BE2;&#x4E0A;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x67E5;&#x8BE2;</td>
</tr>
<tr>
<td style="text-align:left">filter_by()</td>
<td style="text-align:left">&#x628A;&#x7B49;&#x503C;&#x8FC7;&#x6EE4;&#x5668;&#x6DFB;&#x52A0;&#x5230;&#x539F;&#x67E5;&#x8BE2;&#x4E0A;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x67E5;&#x8BE2;</td>
</tr>
<tr>
<td style="text-align:left">limit</td>
<td style="text-align:left">&#x4F7F;&#x7528;&#x6307;&#x5B9A;&#x7684;&#x503C;&#x9650;&#x5B9A;&#x539F;&#x67E5;&#x8BE2;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;</td>
</tr>
<tr>
<td style="text-align:left">offset()</td>
<td style="text-align:left">&#x504F;&#x79FB;&#x539F;&#x67E5;&#x8BE2;&#x8FD4;&#x56DE;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x67E5;&#x8BE2;</td>
</tr>
<tr>
<td style="text-align:left">order_by()</td>
<td style="text-align:left">&#x6839;&#x636E;&#x6307;&#x5B9A;&#x6761;&#x4EF6;&#x5BF9;&#x539F;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x67E5;&#x8BE2;</td>
</tr>
<tr>
<td style="text-align:left">group_by()</td>
<td style="text-align:left">&#x6839;&#x636E;&#x6307;&#x5B9A;&#x6761;&#x4EF6;&#x5BF9;&#x539F;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x8FDB;&#x884C;&#x5206;&#x7EC4;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x67E5;&#x8BE2;</td>
</tr>
</tbody>
</table>
<h3 id="12-&#x5E38;&#x7528;&#x7684;sqlalchemy&#x67E5;&#x8BE2;&#x6267;&#x884C;&#x5668;">1.2 &#x5E38;&#x7528;&#x7684;SQLAlchemy&#x67E5;&#x8BE2;&#x6267;&#x884C;&#x5668;</h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x65B9;&#x6CD5;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">all()</td>
<td style="text-align:left">&#x4EE5;&#x5217;&#x8868;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7684;&#x6240;&#x6709;&#x7ED3;&#x679C;</td>
</tr>
<tr>
<td style="text-align:left">first()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x672A;&#x67E5;&#x5230;&#xFF0C;&#x8FD4;&#x56DE;None</td>
</tr>
<tr>
<td style="text-align:left">first_or_404()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x672A;&#x67E5;&#x5230;&#xFF0C;&#x8FD4;&#x56DE;404</td>
</tr>
<tr>
<td style="text-align:left">get()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4E3B;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x884C;&#xFF0C;&#x5982;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;None</td>
</tr>
<tr>
<td style="text-align:left">get_or_404()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x4E3B;&#x952E;&#x5BF9;&#x5E94;&#x7684;&#x884C;&#xFF0C;&#x5982;&#x4E0D;&#x5B58;&#x5728;&#xFF0C;&#x8FD4;&#x56DE;404</td>
</tr>
<tr>
<td style="text-align:left">count()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x7ED3;&#x679C;&#x7684;&#x6570;&#x91CF;</td>
</tr>
<tr>
<td style="text-align:left">paginate()</td>
<td style="text-align:left">&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Paginate&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x5305;&#x542B;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x5185;&#x7684;&#x7ED3;&#x679C;</td>
</tr>
</tbody>
</table>
<h3 id="2-&#x793A;&#x4F8B;">2. &#x793A;&#x4F8B;</h3>
<h4 id="21-&#x63D2;&#x5165;&#x89D2;&#x8272;&#x6570;&#x636E;">2.1 &#x63D2;&#x5165;&#x89D2;&#x8272;&#x6570;&#x636E;</h4>
<pre><code>ro1 = Role(name=&apos;admin&apos;)
db.session.add(ro1)
db.session.commit()
#&#x518D;&#x6B21;&#x63D2;&#x5165;&#x4E00;&#x6761;&#x6570;&#x636E;
ro2 = Role(name=&apos;user&apos;)
db.session.add(ro2)
db.session.commit()
</code></pre><h4 id="22-&#x4E00;&#x6B21;&#x63D2;&#x5165;&#x591A;&#x6761;&#x6570;&#x636E;">2.2 &#x4E00;&#x6B21;&#x63D2;&#x5165;&#x591A;&#x6761;&#x6570;&#x636E;</h4>
<pre><code class="lang-python">us1 = User(name=<span class="hljs-string">&apos;wang&apos;</span>,email=<span class="hljs-string">&apos;wang@163.com&apos;</span>,password=<span class="hljs-string">&apos;123456&apos;</span>,role_id=ro1.id)
us2 = User(name=<span class="hljs-string">&apos;zhang&apos;</span>,email=<span class="hljs-string">&apos;zhang@189.com&apos;</span>,password=<span class="hljs-string">&apos;201512&apos;</span>,role_id=ro2.id)
us3 = User(name=<span class="hljs-string">&apos;chen&apos;</span>,email=<span class="hljs-string">&apos;chen@126.com&apos;</span>,password=<span class="hljs-string">&apos;987654&apos;</span>,role_id=ro2.id)
us4 = User(name=<span class="hljs-string">&apos;zhou&apos;</span>,email=<span class="hljs-string">&apos;zhou@163.com&apos;</span>,password=<span class="hljs-string">&apos;456789&apos;</span>,role_id=ro1.id)
us5 = User(name=<span class="hljs-string">&apos;tang&apos;</span>,email=<span class="hljs-string">&apos;tang@itheima.com&apos;</span>,password=<span class="hljs-string">&apos;158104&apos;</span>,role_id=ro2.id)
us6 = User(name=<span class="hljs-string">&apos;wu&apos;</span>,email=<span class="hljs-string">&apos;wu@gmail.com&apos;</span>,password=<span class="hljs-string">&apos;5623514&apos;</span>,role_id=ro2.id)
us7 = User(name=<span class="hljs-string">&apos;qian&apos;</span>,email=<span class="hljs-string">&apos;qian@gmail.com&apos;</span>,password=<span class="hljs-string">&apos;1543567&apos;</span>,role_id=ro1.id)
us8 = User(name=<span class="hljs-string">&apos;liu&apos;</span>,email=<span class="hljs-string">&apos;liu@itheima.com&apos;</span>,password=<span class="hljs-string">&apos;867322&apos;</span>,role_id=ro1.id)
us9 = User(name=<span class="hljs-string">&apos;li&apos;</span>,email=<span class="hljs-string">&apos;li@163.com&apos;</span>,password=<span class="hljs-string">&apos;4526342&apos;</span>,role_id=ro2.id)
us10 = User(name=<span class="hljs-string">&apos;sun&apos;</span>,email=<span class="hljs-string">&apos;sun@163.com&apos;</span>,password=<span class="hljs-string">&apos;235523&apos;</span>,role_id=ro2.id)
db.session.add_all([us1,us2,us3,us4,us5,us6,us7,us8,us9,us10])
db.session.commit()
</code></pre>
<h4 id="23-&#x67E5;&#x8BE2;&#x6F14;&#x7EC3;">2.3 &#x67E5;&#x8BE2;&#x6F14;&#x7EC3;</h4>
<p>&#x5B8C;&#x6210;&#x4EE5;&#x4E0B;&#x67E5;&#x8BE2;</p>
<pre><code>1. &#x67E5;&#x8BE2;&#x6240;&#x6709;&#x7528;&#x6237;&#x6570;&#x636E;
2. &#x67E5;&#x8BE2;&#x6709;&#x591A;&#x5C11;&#x4E2A;&#x7528;&#x6237;
3. &#x67E5;&#x8BE2;&#x7B2C;1&#x4E2A;&#x7528;&#x6237;
4. &#x67E5;&#x8BE2;id&#x4E3A;4&#x7684;&#x7528;&#x6237;[3&#x79CD;&#x65B9;&#x5F0F;]
</code></pre><pre><code class="lang-python"><span class="hljs-number">1.</span> &#x67E5;&#x8BE2;&#x6240;&#x6709;&#x7528;&#x6237;&#x6570;&#x636E;
<span class="hljs-comment"># all()&#x8FD4;&#x56DE;&#x67E5;&#x8BE2;&#x5230;&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;</span>
User.query.all()

<span class="hljs-number">2.</span> &#x67E5;&#x8BE2;&#x6709;&#x591A;&#x5C11;&#x4E2A;&#x7528;&#x6237;
User.query.count()

<span class="hljs-number">3.</span> &#x67E5;&#x8BE2;&#x7B2C;<span class="hljs-number">1</span>&#x4E2A;&#x7528;&#x6237;
User.query.first()

<span class="hljs-number">4.</span> &#x67E5;&#x8BE2;id&#x4E3A;<span class="hljs-number">4</span>&#x7684;&#x7528;&#x6237;[<span class="hljs-number">3</span>&#x79CD;&#x65B9;&#x5F0F;]
<span class="hljs-comment"># filter_by&#x76F4;&#x63A5;&#x7528;&#x5C5E;&#x6027;&#x540D;&#xFF0C;&#x6BD4;&#x8F83;&#x7528;=, filter&#x7528;&#x7C7B;&#x540D;.&#x5C5E;&#x6027;&#x540D;&#xFF0C;&#x6BD4;&#x8F83;&#x7528;==</span>
<span class="hljs-comment"># filter_by&#x7528;&#x4E8E;&#x67E5;&#x8BE2;&#x7B80;&#x5355;&#x7684;&#x5217;&#x540D;&#xFF0C;&#x4E0D;&#x652F;&#x6301;&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;</span>
<span class="hljs-comment"># filter&#x6BD4;filter_by&#x7684;&#x529F;&#x80FD;&#x66F4;&#x5F3A;&#x5927;&#xFF0C;&#x652F;&#x6301;&#x6BD4;&#x8F83;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x652F;&#x6301;or_&#x3001;in_&#x7B49;&#x8BED;&#x6CD5;&#x3002;</span>
User.query.get(<span class="hljs-number">4</span>)
User.query.filter_by(id=<span class="hljs-number">4</span>).first()  <span class="hljs-comment">#&#x5C5E;&#x6027; =</span>
User.query.filter(User.id==<span class="hljs-number">4</span>).first() <span class="hljs-comment">#&#x5BF9;&#x8C61;&#x540D;.&#x5C5E;&#x6027; ==</span>
User.query.filter_by(id=<span class="hljs-number">4</span>).first()  <span class="hljs-comment">#&#x5C5E;&#x6027; =</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../chapter3/shu-ju-ku-de-she-zhi.html" class="navigation navigation-prev " aria-label="Previous page: Flask中使用数据库"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../chapter3/zong-he-an-4f8b-tu-shu-guan-li.html" class="navigation navigation-next " aria-label="Next page: 综合案例-图书管理"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 6节课入门Flask框架web开发 -->
</html>
